#include"SList.h"

LinkList ListInit()
{
	struct List* mylist = malloc(sizeof(struct List));
	if (mylist == NULL)
	{
		printf("%s", strerror(errno));
	}
	else
	{
		mylist->Head.next = NULL;
		mylist->m_size = 0;
	}
	

	return mylist;
}

void ListInsert(LinkList list, int pos, void* data)
{
	assert(list);
	assert(data);

	struct List* mylist = list;
	if (pos<0 || pos>mylist->m_size-1)
	{
		pos = mylist->m_size;
	}

	struct LinkNode* MyNode = data;

	struct LinkNode* prev = &mylist->Head;
	for (int i = 0; i < pos; i++)
	{
		prev = prev->next;
	}

	MyNode->next = prev->next;
	prev->next = MyNode;

	mylist->m_size++;

}

void ListPrint(LinkList list,void(*MyPrint)(void*))
{
	struct List* mylist = list;
	struct LinkNode* cur = mylist->Head.next;

	while (cur != NULL)
	{
		MyPrint(cur);
		cur = cur->next;
	}
}

void ListPop(LinkList list, int pos)
{
	struct List* mylist = list;
	struct LinkNode* prev = &mylist->Head;
	for (int i = 0; i < pos; i++)
	{
		prev = prev->next;
	}
	struct LinkNode* del = prev->next;

	prev->next = del->next;

	//free(del);

	mylist->m_size--;
}


void ListDestroy(LinkList list)
{
	assert(list);
	free(list);

	list = NULL;
}
